
/*
 * 文件名：MappingController.java.
 * 描述：
 * 创建人: 龚洪亮
 * 创建时间：2017年8月9日
 */

package com.fiberhome.platform.controller;


import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fiberhome.platform.common.EUDataGridResult;
import com.fiberhome.platform.common.PageResult;
import com.fiberhome.platform.entity.RelationMapDetail;
import com.fiberhome.platform.pojo.RelationMap;
import com.fiberhome.platform.service.RelationMapService;


/**
 * @Description 建立映射相关
 * @author 龚洪亮
 * @version 2017年8月9日
 */
@Controller
public class MappingController {
	@Autowired
	private RelationMapService relationMapService;
	@Autowired
	private HttpSession session;
	@RequestMapping("/map/list")
	@ResponseBody
	public EUDataGridResult getMapList(Integer page, Integer rows) {
		return relationMapService.getMapList(page, rows);
	}
	
	/**
	 * @Description 获取一小时之内建立的映射关系
	 * @Date 2017年8月9日
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/map/listonehour")
	@ResponseBody
	public EUDataGridResult getMapListOneHour(Integer page, Integer rows) {
		return relationMapService.getMapListOneHour(page, rows);
	}
	
	@RequestMapping("/map/isbuild")
	@ResponseBody
	public PageResult isBuild(RelationMap map) {
		return relationMapService.isBuild(map);
		
	}
	
	@RequestMapping("/map/delete")
	@ResponseBody
	public PageResult DeleteMap(String ids) {
		return relationMapService.delteMap(ids);
	}
	
	@RequestMapping("/map/create")
	@ResponseBody
	public PageResult createMap(RelationMap map) {
		return relationMapService.createMap(map);
	}
	
	@RequestMapping("/map/detail")
	public String getMapDetail(Integer id, Model model) {
		RelationMapDetail detail = relationMapService.getMapDetail(id);
		model.addAttribute("need",detail.getDesignNeed());
		model.addAttribute("test", detail.getTestCase());
		return "map-detail";
	}
	
	@RequestMapping("/map/output")
	public String outPut(HttpServletRequest request, HttpServletResponse response, String ids){
		try {
			OutputStream os = null;    
			Workbook wb = relationMapService.writeExcel(ids);    //工作薄
			SimpleDateFormat myFmt1=new SimpleDateFormat("yyyyMMddHHmmss");
			Date now=new Date();
			String fileName = myFmt1.format(now)+".xls";
			//String fileName = "测试项列表.xls";
			response.setContentType("application/vnd.ms-excel");  
			response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(fileName, "utf-8"));  
			os = response.getOutputStream();  
			wb.write(os);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return null; 
	}
	
	@RequestMapping("/map/querymapbycat")
	@ResponseBody
	public EUDataGridResult queryMapByCat(Integer id, Integer page, Integer rows) {
		if (page==null || rows ==null) {
			page=1;
			rows=10;
		}
		if (id==null) {
			Integer session_id = (Integer) session.getAttribute("id");
			if (session_id==null) {
				return relationMapService.getMapList(page, rows);
			} else {
				return relationMapService.queryMapByCat(session_id, page, rows);
			}
		} else {
			session.setAttribute("id", id);
			return relationMapService.queryMapByCat(id, page, rows);
		}
	}
	
}
